home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
PROGRAM
/
REMLIFE.ARJ
/
LIFE.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1989-08-16
|
5KB
|
141 lines
{$R-} {Range checking off}
{$B+} {Boolean complete evaluation on}
{$S+} {Stack checking on}
{$I+} {I/O checking on}
{$N-} {No numeric coprocessor}
{$M 65500,16384,655360} {Turbo 3 default stack and heap}
program life;
{ This is the Game Of Life }
Uses
Crt, {Unit found in TURBO.TPL}
Turbo3; {Unit found in TURBO3.TPU}
const
max = 8; { Maximum number array elements}
max_gen = 10; { Max Generations }
type
r1 = array [1..max,1..max] of integer; { Main type of array }
var
i : integer; { Row Index }
j : integer; { Column Index }
gen : integer; { Generation index }
print_code: integer; { print_code }
board_a : r1; { Main Life Board }
board_b : r1; { Secondary board }
procedure check_life(a:r1; var b:r1; x,y:integer);
var
total_lives : integer;
living : boolean;
Begin
total_lives := 0; { Just to be safe }
total_lives := a[x,y+1] + a[x,y-1] + a[x+1,y] + a[x-1,y] +
a[x+1,y+1] + a[x+1,y-1] + a[x-1,y+1] + a[x-1,y-1];
if a[x,y] = 0 { Determine if person is living there }
then living := false
else living := true;
case living of
true : { If living, then check for survival }
case total_lives of
2,3 : b[x,y] := 1; { Survives }
0,1,4..8 : b[x,y] := 0; { dies }
end;
false : { if not living, then check for birth }
case total_lives of
0..2,4..8: b[x,y] := 0; { Not Born }
3 : b[x,y] := 1; { Born }
end;
end;
End; { Procedure }
procedure print_out(a:r1; gen_num, code:integer);
{ ^matrix, ^generation, ^ what procedure called it. }
var
i,j : integer;
junk : char;
Begin
clrscr;
writeln ('Generation = ',gen_num);
writeln;
for i := 1 to max do
begin
for j := 1 to max do
write (a[i,j]:2);
writeln;
end;
writeln;
writeln;
if (gen_num < max_gen) and (code <> 1)
then begin
write ('Press ENTER to continue.....');
readln (junk)
end;
End;
procedure init; { Initialize the main array and variables }
var
response : char;
tp,tcode : integer;
Begin
for i := 1 to max do
begin
board_a[i,max] := 0; { Initialize the edges to 0 }
board_a[max,i] := 0;
board_a[i,1] := 0;
board_a[1,i] := 0;
end;
board_b := board_a;
tp := 1; { First generation }
tcode := 1; { code the pass }
repeat
for i := 2 to max-1 do
begin
for j := 2 to max-1 do
begin
board_a[i,j] := trunc(random(2)); { Put random lives in array }
board_b[i,j] := 0; { and initialize this one }
end;
end;
print_out(board_a,tp,tcode);
write ('Random Generation, Accept ? (Y/N) --> '); { Random enough? }
read (kbd,response) { Wait 'till pressed }
until (response = 'y') or (response = 'Y'); { and proper response }
End;
BEGIN
init; { Initialize }
print_code := 0; { Main calling }
gen := 1; { First Generation }
repeat { Do the following, }
print_out(board_a,gen,print_code); { First print it }
for i := 2 to max-1 do { Row count }
for j := 2 to max-1 do { Column count }
check_life(board_a,board_b,i,j); { Check life and do stuff }
gen := succ(gen); { Next generation }
board_a := board_b { Old := New }
until gen = max_gen; { Until Generation Count }
print_out(board_a,gen,print_code); { Print one last time }
END.